<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog"
      :custom-class="'panna-dialog_medium'"
      :title="dataList.MenuInformation"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <el-form
        ref="formData"
        :model="formData"
        :rules="formDataRules"
        status-icon
      >
        <el-form-item
          :label="dataList.MenuName"
          :label-width="formLabelWidth"
          prop="name"
          :error="errorMsg.name"
        >
          <el-input v-model="formData.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          :label="dataList.MenuEName"
          :label-width="formLabelWidth"
          prop="eName"
          :error="errorMsg.eName"
        >
          <el-input v-model="formData.eName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          :label="dataList.Icon"
          :label-width="formLabelWidth"
          prop="icon"
        >
          <el-input v-model="formData.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          :label="dataList.FrontEndRoute"
          :label-width="formLabelWidth"
          prop="frontUrl"
          :error="errorMsg.frontUrl"
        >
          <el-input v-model="formData.frontUrl" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          :label="dataList.Component"
          :label-width="formLabelWidth"
          prop="component"
          :error="errorMsg.component"
        >
          <el-input v-model="formData.component" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          :label="dataList.BackEndRoute"
          :label-width="formLabelWidth"
          prop="backendUrl"
          :error="errorMsg.backendUrl"
        >
          <el-input v-model="formData.backendUrl" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          :label="dataList.Sort"
          :label-width="formLabelWidth"
          prop="sort"
          :error="errorMsg.sort"
        >
          <el-input
            v-model="formData.sort"
            type="number"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          :label="dataList.ShowOrNot"
          :label-width="formLabelWidth"
          prop="isShow"
          style="float: left"
        >
          <el-switch
            v-model="formData.isShow"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item
          :label="dataList.EnableOrNot"
          :label-width="formLabelWidth"
          prop="status"
          style="float: left"
        >
          <el-switch
            v-model="formData.status"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button
          type="primary"
          :disabled="loading"
          @click="onSubmit('formData')"
          >{{ btn.Save }}</el-button
        >
        <el-button @click="cancel">{{ btn.Cancel }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "../../../api/Permission/PermissionMenu.js";

export default {
  name: "PermissionMenuEdit",
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      formLabelWidth: "110px",
      initData: {
        id: 0,
        parentId: 0,
        name: "",
        icon: "",
        frontUrl: "",
        backendUrl: "",
        component: "",
        sort: 1,
        isShow: "1",
        status: "1",
      },
      formData: {
        id: 0,
        parentId: 0,
        name: "",
        icon: "",
        frontUrl: "",
        backendUrl: "",
        component: "",
        sort: 1,
        isShow: "1",
        status: "1",
      },
      formDataRules: {
        name: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
          {
            min: 1,
            max: 8,
            message: this.$t("Permission.PermissionUser.TheLength"),
            trigger: "blur",
          },
        ],
        frontUrl: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        backendUrl: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        component: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunction,
          },
        ],
        sort: [
          {
            required: true,
            trigger: "blur",
            validator: this.Helper.validateFunctionNumber,
          },
        ],
      },
      initErrorMsg: {
        name: "",
        frontUrl: "",
        backendUrl: "",
        component: "",
        sort: "",
      },
      errorMsg: {
        name: "",
        frontUrl: "",
        backendUrl: "",
        component: "",
        sort: "",
      },
    };
  },
  computed: {
    btn() {
      return this.$t("Permission.buttons");
    },
    dataList() {
      return this.$t("Permission.PermissionUser");
    },
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  methods: {
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.id <= 0) {
            create(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            update(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    showValidateMsgCheck(res) {
      if (res.msg) {
        this.Helper.showValidateMsg("name", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("frontUrl", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("backendUrl", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("component", this.errorMsg, res.msg);
        this.Helper.showValidateMsg("sort", this.errorMsg, res.msg);
      }
    },
  },
};
</script>
